<?php
class Ptests extends MY_Controller
{
  var $curl = 'ptests';
  var $name = '';
  var $search = '';
  var $language;
  var $submenu = "_menu"; 
  var $id_test = '';
  var $id_industry = '';
  
  function __construct()
  {
    parent::__construct();
        
    $this->load->model(array('/public/test_model', '/public/test_category_model', '/public/nested_sets_model'));
    $this->load->model('/public/category_model', 'cats');

    $this->validation->set_fields($this->test_model->fields);

    $this->data['Title'] = T_('Test Maker');

    $this->language = parent::getLang();
    
    $this->name = T_('Test Maker');   
  }
  
  function index()
  {
  	$config['is_personal'] = 1;
    $this->test_model->search($config);
    $this->data['Title'] .= ' / ' . T_('Personality Tests');
    
    $this->renderLayout();
  }

  function create()
  {
    if (count($_POST))
    {          
      $this->data['categorySelect'] = $this->cats->getCategorySelect("id_parent", $this->input->post('id_parent'));
      $rules = array('name', 'description', 'id_parent', 'time', 'shuffle', 'tags', 'status', 'id_contributor', 'code');
      $this->validation->set_rules($this->test_model->getRules($rules));

      $this->validation->set_message('__nameFree', '<span class="black bold">«'.T_("Test").'»</span> '.T_("already exists with such Name. Please change the Name."));
      $this->validation->set_message('__category', T_('You should select Test Category.'));
      
      if ($this->validation->run() === TRUE)
      {
        $item['name'] 			= $this->validation->name;
        $item['description'] 	= $this->validation->description;
        $item['id_tests_categories'] = $this->validation->id_parent;
        $item['time'] 			= $this->validation->time;
        $item['shuffle'] 		= $this->validation->shuffle;
        $item['tags'] 			= $this->validation->tags;
        $item['status'] 		= $this->validation->status;
        $item['id_contributor'] = $this->validation->id_contributor;
        $item['code'] 			= $this->validation->code;
        $item['is_personal'] 	= 1;
        //$item['reject'] 		= $this->validation->reject;
        
        if ($id_test = $this->test_model->insert($item))
        {
          redirect($this->curl . '/index'); 
        }       
      }
    }
    else
    {
    	$this->data['categorySelect'] = $this->cats->getCategorySelect("id_parent");
    } 

    $this->data['Title'] .= ' / ' . T_('Personality Tests') . ' / New Test';
    $this->renderLayout();    
  }
  
  function edit($id = null)
  {
    if (!empty($id))
    {     
      $object = $this->test_model->get($id);

      if (!count($object))
      {
        $this->data['view'] = '_elements/query_no_object';
        $this->load->view($this->data['layout'], $this->data);
  
        return false; 
      }
      
      $this->data['name_contributor'] = $object['name_contributor'];

      if (!count($_POST))
      {
        $this->validation->id   		= $object['id']; 
        $this->validation->name 		= $object['name'];
        $this->validation->description 	= $object['description'];
        $this->validation->id_tests_categories = $object['id_tests_categories'];
        $this->validation->time 		= $object['time'];
        $this->validation->shuffle 		= $object['shuffle'];
        $this->validation->type 		= $object['type'];
        $this->validation->value 		= $object['value'];
        $this->validation->tags 		= $object['tags'];
        $this->validation->status 		= $object['status'];
        $this->validation->id_contributor = $object['id_contributor'];
        $this->validation->code 		= $object['code'];
        $this->validation->private 		= $object['private'];
        $this->validation->reject 		= $object['reject'];
        $this->data['categorySelect'] = $this->cats->getCategorySelect("id_parent", $object['id_tests_categories']);
      }
      else
      {
        $this->id_test = $id;
        $this->data['categorySelect'] = $this->cats->getCategorySelect("id_parent", $this->input->post('id_parent'));
        
       	$rules = array('name', 'description', 'id_parent', 'time', 'shuffle', 'tags', 'status', 'id_contributor', 'code', 'reject');
        $this->validation->set_rules($this->test_model->getRules($rules));
        
        $this->validation->set_message('__nameFree', '<span class="black bold">«'.T_("Test").'»</span> '.T_("already exists with such Name. Please change the Name."));
        $this->validation->set_message('__category', T_('You should select Test Category.'));
        
        if ($this->validation->run() === TRUE)
        {
          	$item['id']   			= (int)$this->validation->id;
 			$item['name'] 			= $this->validation->name;
	        $item['description'] 	= $this->validation->description;
	        $item['id_tests_categories'] = $this->validation->id_parent;
	        $item['time'] 			= $this->validation->time;
	        $item['shuffle'] 		= $this->validation->shuffle;
	        $item['tags'] 			= $this->validation->tags;
	        $item['status'] 		= $this->validation->status;
	        $item['id_contributor'] = $this->validation->id_contributor;
	        $item['code'] 			= $this->validation->code;
	        $item['reject'] 		= $this->validation->reject;
          
            $this->test_model->update($item);

            redirect($this->curl . '/index');
        }
      }     
    }
    
    $this->data['Title'] .= ' / ' . T_('Personality Tests') . ' / ' . T_('Edit') . ' / ' . $object['name'];
    $this->renderLayout();
  }
  
  function deleteItems()
  {
    $data = $this->test_model->delete($_REQUEST['items']);

    echo $data;
  }

  function delete($id)
  {
    $data = $this->test_model->delete($id);

    if ($data)
    {
      redirect($this->curl . '/index'); 
    }
    else
    {
      redirect($this->curl . '/edit/'. $id);
    }
  }
    
  function __nameFree($name = null)
  { 
    return $this->test_model->nameFree($name);
  }

	function __category($id) 
	{
	   if ($id == 1) return false;
	   else return true;
	}
}

?>